﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Link_LinkMain : System.Web.UI.UserControl
{
    private int nDefLang = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        //Alert1.HideMessage();
        if (!IsPostBack)
        {
            this.SetLableLang();
            this.ShowMess();
            this.BindDataToGrid(1);
        }
    }

    private void SetLableLang()
    {
       
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefLang - 1];
        
    }


    #region Paging & Message


    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:

                break;
        }
    }
    #endregion

    #region Grid Processing
    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nPageCount = 0;
        Link objLink = new Link();
        DataTable dtbLink = new DataTable();
        dtbLink = objLink.Search();
        RpLink.DataSource = dtbLink;
        RpLink.DataBind();
        nPageCount=RpLink.Items.Count;
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefLang - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefLang - 1];
    }


    protected void RpLink_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Link objLink = new Link();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objLink.DeleteById(nID);
                //if (nDelete > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLang]);
                //else
                //    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLang]);
                BindDataToGrid(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_LINK_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

            case "Active":
                int nActive = objLink.UpdateStatus(nID, EnumeType.INACTIVE);
                //if (nActive > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[nDefaultLang]);

                //else
                //    Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[nDefaultLang]);
                BindDataToGrid(1);
                break;

            case "Inactive":
                int nInactive = objLink.UpdateStatus(nID, EnumeType.ACTIVE);
                //if (nInactive > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[nDefaultLang]);
                //else
                //    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[nDefaultLang]);
                BindDataToGrid(1);
                break;
        }
    }
    protected void RpLink_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblPartner = (Label)e.Item.FindControl("lblPartner");
            Label lblUrl = (Label)e.Item.FindControl("lblUrl");
            Label lblDateCreated = (Label)e.Item.FindControl("lblDateCreated");

            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            lblUrl.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "URL"));
            lblPartner.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Name"));

            lblDateCreated.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CreatedDate")));

            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            if (nStatus > 0)
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }

            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "LinkID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLang]);
            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "LinkID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaultLang]);
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "LinkID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLang]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "LinkID"));
        }
    }
    
    #endregion

    #region Click Events
  
   
    protected void btnAddnew_Click(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_LINK_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
    #endregion

   
}